package com.kotlin.flong.coroutines

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

/**
 * @Author:liangjl
 * @Date: 2020/7/7-16:39
 * @Eamil:jilongliang@sina.com
 * @Description:
 * 线程于协程性能对比
 */
fun main() = runBlocking{
    myThread()
    //myThread1()
}


//耗时：917毫秒
private suspend fun  myThread1(){
    var startTime = System.currentTimeMillis()
    var threadList = List(100000) {

        GlobalScope.launch {
            println("MyThread")
        }
    }

    threadList.forEach { it.join() }
    var endTime = System.currentTimeMillis()

    println("耗时：" + (endTime - startTime) + "毫秒")

}
//耗时：7662毫秒
private fun myThread() {
    var startTime = System.currentTimeMillis()
    var threadList = List(100000) {
        MyThread()
    }

    threadList.forEach { it.start() }
    threadList.forEach { it.join() }
    var endTime = System.currentTimeMillis()

    println("耗时：" + (endTime - startTime) + "毫秒")
}

class MyThread : Thread() {
    override fun run() {
        println("MyThread")
    }
}

